package com.jzy.secondhandbooktradingmanagement.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzy.secondhandbooktradingmanagement.commom.Result;
import com.jzy.secondhandbooktradingmanagement.entity.User;
import com.jzy.secondhandbooktradingmanagement.service.IUserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    IUserService userService;

    @GetMapping("/test")
    public String test(){
        return "hello world";
    }

    @PostMapping("/register")
    public Result<?> register(@RequestBody User user){
        try {
            userService.register(user);
            return Result.success();
        } catch (Exception e) {
            return Result.error("-1", e.getMessage());
        }
    }
    @CrossOrigin
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user){
        try {
            User res = userService.login(user);
            return Result.success(res);
        } catch (Exception e) {
            return Result.error("-1", e.getMessage());
        }
    }
    @PostMapping
    public Result<?> save(@RequestBody User user){
        if(user.getPassword() == null){
            user.setPassword("abc123456");
        }
        userService.save(user);
        return Result.success();
    }
    @PutMapping("/password")
    public  Result<?> update( @RequestParam(name = "id") Integer id,
                              @RequestParam(name = "password2") String password2){
        userService.updatePassword(id, password2);
        return Result.success();
    }
    @PutMapping
    public  Result<?> password(@RequestBody User user){
        userService.updateById(user);
        return Result.success();
    }
    @PostMapping("/deleteBatch")
    public  Result<?> deleteBatch(@RequestBody List<Integer> ids){
        userService.removeBatchByIds(ids);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        userService.removeById(id);
        return Result.success();
    }
    @GetMapping
    public Result<?> findPage(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(name = "search", defaultValue = "") String search){
        Page<User> userPage = userService.findPage(pageNum, pageSize, search);
        return Result.success(userPage);
    }
    @GetMapping("/usersearch")
    public Result<?> findPage2(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(name = "search1", defaultValue = "") String search1,
                               @RequestParam(name = "search2", defaultValue = "") String search2,
                               @RequestParam(name = "search3", defaultValue = "") String search3,
                               @RequestParam(name = "search4", defaultValue = "") String search4){
        Page<User> userPage = userService.findPage2(pageNum, pageSize, search1, search2, search3, search4);
        return Result.success(userPage);
    }
}
